﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using StockFinder.Model.Enumerations;

namespace StockFinder.Model
{
    public class Symbol
    {
        public int SymbolId { get; set; }
        public string Name { get; set; }
        public DateTime CreatedDateTime { get; set; }
        public Exchange Exchange { get; set; }
        public bool HasLoadedPrices { get; set; }

        public List<IBDRatings> IBDRatingsHistory { get; set; }
        public List<DailyPrice> Prices { get; set; }
        public List<WeeklyPrice> WeeklyPrices { get; set; }

        public Symbol()
        {
            WeeklyPrices = new List<WeeklyPrice>();
            Prices = new List<DailyPrice>();
        }

        public WeeklyPrice GetExistingWeeklyPrice(int weekOfYear, int year)
        {
            WeeklyPrice matchingWeeklyPrice = null;

            foreach (WeeklyPrice weeklyPrice in WeeklyPrices)
            {
                if (weeklyPrice.WeekOfYear == weekOfYear &&
                    weeklyPrice.Year == year)
                {
                    matchingWeeklyPrice = weeklyPrice;
                }
            }

            return matchingWeeklyPrice;
        }

        /// <summary>
        /// Generates the weekly price data based on daily prices list
        /// </summary>
        /// <param name="symbol"></param>
        public void GenerateWeeklyPrices()
        {
            //make sure we have some prices
            if (Prices != null && Prices.Count > 0)
            {
                WeeklyPrices.Clear();

                //loop the daily prices
                foreach (DailyPrice dailyPrice in Prices)
                {
                    //duff prices?
                    if (dailyPrice.Open == 0)
                    {
                        //yes so quit
                        break;
                    }

                    //get the week of year
                    int weekOfYear = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(
                        dailyPrice.PriceDate,
                        CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule,
                        CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);

                    //does this week/year combination already exist?
                    WeeklyPrice weeklyPrice = GetExistingWeeklyPrice(weekOfYear, dailyPrice.PriceDate.Year);

                    //does it exist?
                    if (weeklyPrice == null)
                    {
                        //no so create and setup
                        weeklyPrice = new WeeklyPrice();
                        weeklyPrice.Year = dailyPrice.PriceDate.Year;
                        weeklyPrice.WeekOfYear = weekOfYear;

                        //add to list
                        WeeklyPrices.Add(weeklyPrice);
                    }

                    //set day of week
                    switch (dailyPrice.PriceDate.DayOfWeek)
                    {
                        case DayOfWeek.Monday:

                            weeklyPrice.Monday = dailyPrice;

                            break;

                        case DayOfWeek.Tuesday:

                            weeklyPrice.Tuesday = dailyPrice;

                            break;

                        case DayOfWeek.Wednesday:

                            weeklyPrice.Wednesday = dailyPrice;

                            break;

                        case DayOfWeek.Thursday:

                            weeklyPrice.Thursday = dailyPrice;

                            break;

                        case DayOfWeek.Friday:

                            weeklyPrice.Friday = dailyPrice;

                            break;

                        default:
                            //do nothing
                            break;
                    }

                }
            }
        }
    }
}
